﻿Imports System.Xml

Public Class Form1

    Dim gcolor = New Color() {Color.LightBlue, Color.Red, Color.Blue, Color.Black, Color.White}
    Dim playerState = New String() {"playing", "paused", "loading", "stop"}
    Dim nowcolor
    Dim APP_ID As String = "YigeSL"
    Dim FW As Integer = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Width     '屏幕宽
    Dim FH As Integer = System.Windows.Forms.SystemInformation.PrimaryMonitorSize.Height    '屏幕高
    Dim Ac As Integer = 10   '准确度

    Declare Function SetWindowPos Lib "user32" Alias "SetWindowPos" (ByVal hwnd As Integer, ByVal hWndInsertAfter As Integer, ByVal x As Integer, ByVal y As Integer, ByVal cx As Integer, ByVal cy As Integer, ByVal wFlags As Integer) As Integer
    Const GW_CHILD = 5
    Const GW_HWNDNEXT = 2
    Const SWP_NOMOVE = 2
    Const SWP_NOSIZE = 1
    Const FLAGS = SWP_NOMOVE Or SWP_NOSIZE
    Const HWND_TOPMOST = -1
    Const HWND_NOTOPMOST = -2

    Dim title(3) As String

    '状态 - 歌曲名 - 时间 - “亦歌”
    Private Sub Form1_Move(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Move
        If Me.Top > -Ac AndAlso Me.Top < Ac Then
            Me.Top = 0
        End If
        If Me.Bottom > FH - Ac AndAlso Me.Bottom < FH + Ac Then
            Me.Top = FH - Me.Height
        End If
        If (Me.Left < Ac AndAlso Me.Left > -Ac) Then
            Me.Left = 0
        End If
        If Me.Right > FW - Ac AndAlso Me.Right < FW + Ac Then
            Me.Left = FW - Me.Width
        End If
        Application.DoEvents()
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        On Error GoTo errors
        System.Threading.Thread.Sleep(1000)
        If My.Application.CommandLineArgs.Count <> 0 Then
            Dim args = My.Application.CommandLineArgs
            For i = 0 To args.Count - 1
                sendCommand("command", args(i))
            Next
            End
        End If
        Dim po As Point
        po.Y = Screen.AllScreens(0).WorkingArea.Height - 50
        ' po.X = (Screen.AllScreens(0).WorkingArea.Width - Me.Width) / 2
        Me.Location = po
        Me.Width = Screen.AllScreens(0).WorkingArea.Width
        title(3) = "亦歌"
        SetWindowPos(Me.Handle, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
        Remote.Movie = Application.StartupPath & "\commander.swf"
        SetWindowPos(Me.Handle, HWND_TOPMOST, 0, 0, 0, 0, FLAGS)
        Exit Sub
errors:
        MsgBox("发生常规性错误!请重新启动程序" & vbCrLf & Err.Description)
        End
    End Sub
    Public Sub Update_NowTime(ByVal playhead)
        Update_Title()
        title(2) = playhead
    End Sub

    Public Sub Update_Lrc(ByVal currentsong)
        Label3.Text = currentsong(3)
        Update_Title()
    End Sub

    Public Sub Update_Info(ByVal currentsong)
        title(1) = currentsong(1) & " - " & currentsong(0)
        Label3.Text = title(1)
        Update_Title()

        NotifyIcon1.ShowBalloonTip(1000, "歌曲更换咯!", "现在播放的歌曲是：" & title(1), ToolTipIcon.Info)
        ToolStripComboBox3.Items.Add(title(1))
    End Sub

    Public Sub Update_Style(ByVal colorStyle)

        Select Case colorStyle
            Case "lightBlue"
                Me.BackColor = gcolor(0)
            Case "blue"
                Me.BackColor = gcolor(1)
            Case "red"
                Me.BackColor = gcolor(2)
            Case "black"
                Me.BackColor = gcolor(3)
            Case "google"
                Me.BackColor = gcolor(4)
        End Select
    End Sub

    Public Sub Update_State(ByVal nowState)
        Select Case nowState
            Case "playing"
                PictureBox2.Image = My.Resources.pause1
            Case "paused"
                PictureBox2.Image = My.Resources.play1
        End Select
        Update_Title()
    End Sub

    Sub Update_Title()
        Me.Text = title(1) + " - " + title(2) + " - " + title(3)
        Me.ToolTip1.SetToolTip(PictureBox3, title(1) + " - " + title(2) + " - " + title(3))

    End Sub

    'Dim jumplist As New Windows7.DesktopIntegration.JumpListManager(APP_ID)

    Private Sub Form1_Shown(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Shown

        'If MS.WindowsAPICodePack.Internal.CoreHelpers.RunningOnWin7 Then
        '    Windows7.DesktopIntegration.Windows7Taskbar.SetCurrentProcessAppId(APP_ID)
        '    Windows7.DesktopIntegration.Windows7Taskbar.SetWindowAppId(Me.Handle, APP_ID)
        '    Dim task As New Windows7.DesktopIntegration.ShellLink
        '    task.Title = "播放/暂停"
        '    task.Path = Application.ExecutablePath
        '    task.Arguments = "playPause"
        '    jumplist.AddUserTask(task)
        '    task = New Windows7.DesktopIntegration.ShellLink
        '    task.Title = "下一首"
        '    task.Path = Application.ExecutablePath
        '    task.Arguments = "next"
        '    jumplist.AddUserTask(task)
        '    task = New Windows7.DesktopIntegration.ShellLink
        '    task.Title = "静音/恢复声音"
        '    task.Path = Application.ExecutablePath
        '    task.Arguments = "volumeOnOff"
        '    jumplist.AddUserTask(task)
        'End If
        sendCommand("command", "openLyricWindow", True)
        sendCommand("command", "getStatus", "currentSongInfo")
        sendCommand("command", "getStatus", "playerState")

    End Sub

    Public playhead As String
    'Dim qianqian As New Form1
    Public currentsong(3)
    Public Sub AxShockwaveFlash1_FlashCall(ByVal sender As Object, ByVal e As AxShockwaveFlashObjects._IShockwaveFlashEvents_FlashCallEvent) Handles Remote.FlashCall
        Dim innerText As String = Me.nodeXml(e.request.ToString())(0).ChildNodes(0).InnerText
        Dim str2 As String = Nothing
        Dim str3 As String = innerText
        If (str3 IsNot Nothing) AndAlso (str3 <> "playheadUpdate") Then
            If Not (str3 = "playerState") Then
                If str3 = "currentSongInfo" Then
                    Dim node As XmlNode = Me.nodeXml(e.request.ToString())(0).ChildNodes.Item(1)
                    For Each element As XmlElement In node
                        If element.GetAttribute("id") = "name" Then
                            currentsong(1) = element.InnerText
                        End If
                        If element.GetAttribute("id") = "album" Then
                            currentsong(2) = element.InnerText
                        End If
                        If element.GetAttribute("id") = "singer" Then
                            currentsong(0) = element.InnerText
                        End If
                    Next
                    Update_Info(currentsong)
                ElseIf (str3 = "lyricSentence") Then 'AndAlso (Me.iTipsStyleMode = 1) Then
                    currentsong(3) = Me.nodeXml(e.request.ToString())(0).ChildNodes(1).ChildNodes(0).InnerText.ToString()
                    Update_Lrc(currentsong)
                ElseIf str3 = "playhead" Then
                    Dim node As XmlNode = Me.nodeXml(e.request.ToString())(0).ChildNodes.Item(1)
                    For Each element As XmlElement In node
                        If element.GetAttribute("id") = "currentTime" Then
                            playhead = element.InnerText
                        End If
                    Next
                    Dim playhead3 = playhead.Split(".")
                    Dim playhead2 As Integer = playhead3(0)
                    Dim times(1) As Integer
                    times(0) = playhead2 / 60
                    times(1) = playhead2 - (times(0) * 60)
                    If times(1) < 0 Then
                        times(0) -= 1
                        times(1) += 60
                    End If
                    Update_NowTime(times(0) & " : " & times(1))
                ElseIf str3 = "colorStyle" Then
                    Update_Style(Me.nodeXml(e.request.ToString())(0).ChildNodes(1).ChildNodes(0).InnerText.ToString())
                End If
            Else
                str2 = Me.nodeXml(e.request.ToString())(0).ChildNodes(1).ChildNodes(0).InnerText.ToString()
                If (str2 = "paused") OrElse ((str2 <> "playing") AndAlso Not (str2 = "loading")) Then
                    Update_State(str2)
                End If
            End If
        End If
    End Sub
    Public Function nodeXml(ByVal s As String) As XmlNodeList
        Dim document As New XmlDocument()
        document.LoadXml(s)
        Return document.GetElementsByTagName("arguments")
    End Function

    Public Sub sendCommand(ByVal type As String, ByVal value As String)
        On Error Resume Next
        Remote.CallFunction(("<invoke name=""" & type & """ returntype=""xml""><arguments><string>") + value & "</string></arguments><arguments></arguments></invoke>")
    End Sub

    Public Sub sendCommand(ByVal type As String, ByVal value1 As String, ByVal value2 As String)
        On Error Resume Next
        Remote.CallFunction((("<invoke name=""" & type & """ returntype=""xml""><arguments><string>") + value1 & "</string></arguments><arguments><string>") + value2 & "</string></arguments></invoke>")
    End Sub





    '#Region "Lavel3 Move"

    '    Dim down5 As Boolean = False
    '    '记录鼠标开始位置   
    '    Dim mousePos5 As Point

    '    Private Sub Label3_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label3.MouseDown
    '        down5 = True
    '        mousePos5 = New Point(e.X, e.Y)
    '    End Sub

    '    Private Sub Label3_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label3.MouseUp
    '        down5 = False
    '    End Sub

    '    Private Sub Label3_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Label3.MouseMove
    '        If (down5) Then
    '            '   根据鼠标的移动，实际移动控件   
    '            Dim diffx, diffy As Integer
    '            diffx = mousePos5.X - e.X
    '            diffy = mousePos5.Y - e.Y
    '            Me.Label3.Location = New Point(Me.Label3.Location.X - diffx, Me.Label3.Location.Y - diffy)
    '        End If
    '    End Sub
    '#End Region

    '#Region "PictureBox2 Move"

    '    Dim down3 As Boolean = False
    '    '记录鼠标开始位置   
    '    Dim mousePos3 As Point

    '    Private Sub PictureBox2_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseDown
    '        down3 = True
    '        mousePos3 = New Point(e.X, e.Y)
    '    End Sub

    '    Private Sub PictureBox2_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseUp
    '        down3 = False
    '    End Sub

    '    Private Sub PictureBox2_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox2.MouseMove
    '        If (down3) Then
    '            '   根据鼠标的移动，实际移动控件   
    '            Dim diffx, diffy As Integer
    '            diffx = mousePos3.X - e.X
    '            diffy = mousePos3.Y - e.Y
    '            Me.PictureBox2.Location = New Point(Me.PictureBox2.Location.X - diffx, Me.PictureBox2.Location.Y - diffy)
    '        End If
    '    End Sub
    '#End Region

    '#Region "PictureBox1 Move"

    '    Dim down4 As Boolean = False
    '    '记录鼠标开始位置   
    '    Dim mousePos4 As Point

    '    Private Sub PictureBox1_MouseDown(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseDown
    '        down4 = True
    '        mousePos4 = New Point(e.X, e.Y)
    '    End Sub

    '    Private Sub PictureBox1_MouseUp(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseUp
    '        down4 = False
    '    End Sub

    '    Private Sub PictureBox1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles PictureBox1.MouseMove
    '        If (down4) Then
    '            '   根据鼠标的移动，实际移动控件   
    '            Dim diffx, diffy As Integer
    '            diffx = mousePos4.X - e.X
    '            diffy = mousePos4.Y - e.Y
    '            Me.PictureBox1.Location = New Point(Me.PictureBox1.Location.X - diffx, Me.PictureBox1.Location.Y - diffy)
    '        End If
    '    End Sub
    '#End Region

    Private Sub PictureBox2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox2.Click, 播放暂停ToolStripMenuItem.Click
        sendCommand("command", "playPause")
    End Sub

    Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click, 下一首ToolStripMenuItem.Click
        sendCommand("command", "next")
    End Sub

    Private Sub Show1gcommanderToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Show1gcommanderToolStripMenuItem.Click
        Remote.Visible = True
    End Sub

    Private Sub ReloadSwfToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ReloadSwfToolStripMenuItem.Click
        Remote.Movie = Application.StartupPath & "\commander.swf"
        sendCommand("command", "openLyricWindow", True)
        sendCommand("command", "getStatus", "currentSongInfo")
        sendCommand("command", "getStatus", "playerState")
    End Sub

    Private Sub 静音恢复ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 静音恢复ToolStripMenuItem.Click
        sendCommand("command", "volumeOnOff")
    End Sub

    Private Sub 锁定当前播放歌曲ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 锁定当前播放歌曲ToolStripMenuItem.Click
        sendCommand("command", "lockCurrent")
    End Sub

    Private Sub 收藏当前播放歌曲ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 收藏当前播放歌曲ToolStripMenuItem.Click
        sendCommand("command", "saveCurrent")
    End Sub

    Private Sub LightblueToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles LightblueToolStripMenuItem.Click
        sendCommand("command", "changeColorStyle", "lightBlue")
    End Sub

    Private Sub RedToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles RedToolStripMenuItem.Click
        sendCommand("command", "changeColorStyle", "red")
    End Sub

    Private Sub BlueToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlueToolStripMenuItem.Click
        sendCommand("command", "changeColorStyle", "blue")
    End Sub

    Private Sub BlackToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BlackToolStripMenuItem.Click
        sendCommand("command", "changeColorStyle", "black")
    End Sub

    Private Sub GoogleToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GoogleToolStripMenuItem.Click
        sendCommand("command", "changeColorStyle", "google")
    End Sub

    Private Sub ToolStripComboBox1_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ToolStripComboBox1.KeyPress

        If e.KeyChar = Chr(13) Then
            sendCommand("command", "play", ToolStripComboBox1.Text)
            ToolStripComboBox1.Items.Add(ToolStripComboBox1.Text)
            Exit Sub
        Else : Exit Sub
        End If
    End Sub

    Private Sub PictureBox3_MouseHover(ByVal sender As Object, ByVal e As System.EventArgs) Handles PictureBox3.MouseHover
        Me.Focus()
    End Sub

    Private Sub 退出ToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles 退出ToolStripMenuItem.Click
        Me.Close()
        End
    End Sub

    Private Sub ToolStripComboBox2_KeyPress(ByVal sender As Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles ToolStripComboBox2.KeyPress
        If e.KeyChar = Chr(13) Then
            Dim result As New searchresult
            result.query = ToolStripComboBox2.Text
            NotifyIcon1.ShowBalloonTip(1500, "小提示!", "小提示说：我正在搜索歌曲哦。关键字为：" & result.query, ToolTipIcon.Info)
            ToolStripComboBox2.Items.Add(ToolStripComboBox2.Text)
            result.Show()
            Exit Sub
        Else : Exit Sub
        End If
    End Sub

    Private Sub ToolStripComboBox3_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ToolStripComboBox3.SelectedIndexChanged
        sendCommand("command", "play", ToolStripComboBox3.SelectedItem(0).ToString)
        ToolStripComboBox1.Items.Add(ToolStripComboBox3.SelectedItem(0).ToString)
    End Sub
End Class
